package com.chenyue.cm.match.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.match.dao.JdMatchGroupMapper;
import com.chenyue.cm.match.dao.JdMatchGroupUserMapper;
import com.chenyue.cm.match.dao.JdMatchSignMapper;
import com.chenyue.cm.match.domain.JdMatchGroup;
import com.chenyue.cm.match.domain.JdMatchGroupUser;
import com.chenyue.cm.match.domain.JdMatchSign;
import com.chenyue.cm.match.service.JdMatchGroupService;
import com.chenyue.cm.match.vo.GroupUserList;
import com.chenyue.cm.user.dao.JdAppUserMapper;
import com.chenyue.cm.user.domain.JdAppUser;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author chenyue-cm
 */
@Service(value = "JdMatchGroupService")
public class JdMatchGroupServiceImpl extends BaseServiceImpl<JdMatchGroup> implements JdMatchGroupService {

    private final JdMatchGroupMapper jdMatchGroupMapper;

    private final JdMatchGroupUserMapper jdMatchGroupUserMapper;

    private final JdMatchSignMapper matchSignMapper;

    private final JdAppUserMapper jdAppUserMapper;

    public JdMatchGroupServiceImpl(@Qualifier("jdMatchGroupMapper") JdMatchGroupMapper jdMatchGroupMapper,
                                   @Qualifier("jdMatchGroupUserMapper") JdMatchGroupUserMapper jdMatchGroupUserMapper,
                                   JdMatchSignMapper matchSignMapper, @Qualifier("jdAppUserMapper") JdAppUserMapper jdAppUserMapper) {
        this.jdMatchGroupMapper = jdMatchGroupMapper;
        this.jdMatchGroupUserMapper = jdMatchGroupUserMapper;
        this.matchSignMapper = matchSignMapper;
        this.jdAppUserMapper = jdAppUserMapper;
    }

    @Override
    @Resource(name = "jdMatchGroupMapper")
    public void setBaseMapper(LcBaseMapper<JdMatchGroup, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public List<GroupUserList> groupUsers(Integer matchId) {
        List<GroupUserList> returnList = new ArrayList<>();
        List<JdMatchGroup> list = lambdaQuery()
                .eq(JdMatchGroup::getMatchId, matchId)
                .list();
        for (JdMatchGroup jdMatchGroup : list) {
            GroupUserList groupUser = new GroupUserList();
            groupUser.setGroupId(jdMatchGroup.getId());
            groupUser.setGroupName(jdMatchGroup.getName());
            List<GroupUserList.User> userList = new ArrayList<>();

            List<JdMatchGroupUser> jdMatchGroupUsers = jdMatchGroupUserMapper.selectList(
                    new LambdaQueryWrapper<JdMatchGroupUser>()
                            .eq(JdMatchGroupUser::getGroupId, jdMatchGroup.getId())
            );
            List<Integer> userIds = jdMatchGroupUsers.stream().map(JdMatchGroupUser::getUserId).collect(Collectors.toList());
            List<JdAppUser> jdAppUsers = new ArrayList<>();
            if (userIds.size() > 0) {
                jdAppUsers = jdAppUserMapper.selectBatchIds(userIds);
            }
            for (JdAppUser jdAppUser : jdAppUsers) {
                GroupUserList.User user = new GroupUserList.User();
                user.setUserId(Integer.valueOf(jdAppUser.getId()));
                user.setUserName(jdAppUser.getUserName());
                user.setUserHeadImg(jdAppUser.getHeadImg());
                userList.add(user);
            }
            groupUser.setUserList(userList);
            returnList.add(groupUser);
        }
        return returnList;
    }

    /**
     * 单场赛事下的参赛团队参赛员列表
     *
     * @param matchId 赛事
     * @return 参赛团队用户列表
     */
    @Override
    public List<GroupUserList.User> userList(Integer matchId) {
        List<GroupUserList.User> userList = new ArrayList<>();
        final List<JdMatchSign> signList = matchSignMapper.selectList(
                new LambdaQueryWrapper<JdMatchSign>()
                        .eq(JdMatchSign::getMatchId, matchId)
        );
        List<Integer> userIds = signList.stream().map(JdMatchSign::getUserId).collect(Collectors.toList());
        List<JdAppUser> jdAppUsers = new ArrayList<>();
        if (userIds.size() > 0) {
            jdAppUsers = jdAppUserMapper.selectBatchIds(userIds);
        }
        for (JdAppUser jdAppUser : jdAppUsers) {
            GroupUserList.User user = new GroupUserList.User();
            user.setUserId(Integer.valueOf(jdAppUser.getId()));
            user.setUserName(jdAppUser.getUserName());
            user.setUserHeadImg(jdAppUser.getHeadImg());
            userList.add(user);
        }
        return userList;
    }
}
